package
{
	import com.ngame.core.conf.data.ActiveConf;
	import com.ngame.core.conf.data.BattleAnalysisConf;
	import com.ngame.core.conf.data.BdSkillConf;
	import com.ngame.core.conf.data.BuffConf;
	import com.ngame.core.conf.data.CSkillBufferConf;
	import com.ngame.core.conf.data.CommonConstantConf;
	import com.ngame.core.conf.data.CommonNumPriceConf;
	import com.ngame.core.conf.data.CommonRewardConf;
	import com.ngame.core.conf.data.ComposeConf;
	import com.ngame.core.conf.data.DailyActivityConf;
	import com.ngame.core.conf.data.DailyTaskConf;
	import com.ngame.core.conf.data.DailyTaskRewardConf;
	import com.ngame.core.conf.data.EnchantCostConf;
	import com.ngame.core.conf.data.EnchantProConf;
	import com.ngame.core.conf.data.ErrorCodeConf;
	import com.ngame.core.conf.data.ExchangeShopConf;
	import com.ngame.core.conf.data.FashionConf;
	import com.ngame.core.conf.data.FashionUplevelConf;
	import com.ngame.core.conf.data.FighterPropertyConf;
	import com.ngame.core.conf.data.FushuConf;
	import com.ngame.core.conf.data.FushuUplevelConf;
	import com.ngame.core.conf.data.FzSkillConf;
	import com.ngame.core.conf.data.GameStepConf;
	import com.ngame.core.conf.data.GameTaskConf;
	import com.ngame.core.conf.data.GuildBattleRewardConf;
	import com.ngame.core.conf.data.GuildConf;
	import com.ngame.core.conf.data.GuildDonateConf;
	import com.ngame.core.conf.data.GuildSkillConf;
	import com.ngame.core.conf.data.GuildTaskConf;
	import com.ngame.core.conf.data.GuildTaskRewardConf;
	import com.ngame.core.conf.data.InstanceConf;
	import com.ngame.core.conf.data.ItemConf;
	import com.ngame.core.conf.data.ItemExtConf;
	import com.ngame.core.conf.data.ItemPackageConf;
	import com.ngame.core.conf.data.JctConf;
	import com.ngame.core.conf.data.LevelInvestConf;
	import com.ngame.core.conf.data.LuckyTreasuryShowConf;
	import com.ngame.core.conf.data.MapConf;
	import com.ngame.core.conf.data.MapEntryConf;
	import com.ngame.core.conf.data.MarryInsConf;
	import com.ngame.core.conf.data.MarryLoveConf;
	import com.ngame.core.conf.data.MarryUpConf;
	import com.ngame.core.conf.data.MenuAuthorityConf;
	import com.ngame.core.conf.data.MessageConf;
	import com.ngame.core.conf.data.ModuleLinkConf;
	import com.ngame.core.conf.data.MonsterConf;
	import com.ngame.core.conf.data.MonsterSkillConf;
	import com.ngame.core.conf.data.NpcConf;
	import com.ngame.core.conf.data.OpenActivityConf;
	import com.ngame.core.conf.data.PetChangejobConf;
	import com.ngame.core.conf.data.PlayerConf;
	import com.ngame.core.conf.data.PromptConf;
	import com.ngame.core.conf.data.QyConf;
	import com.ngame.core.conf.data.RealmConf;
	import com.ngame.core.conf.data.RewardInfoConf;
	import com.ngame.core.conf.data.SceneMonsterConf;
	import com.ngame.core.conf.data.SceneSkillConf;
	import com.ngame.core.conf.data.ShopConf;
	import com.ngame.core.conf.data.SkillBuffConf;
	import com.ngame.core.conf.data.SkillProcConf;
	import com.ngame.core.conf.data.StrongCostConf;
	import com.ngame.core.conf.data.StrongGuideConf;
	import com.ngame.core.conf.data.StrongProConf;
	import com.ngame.core.conf.data.SystemDescriptionConf;
	import com.ngame.core.conf.data.SzSkillConf;
	import com.ngame.core.conf.data.TitleConf;
	import com.ngame.core.conf.data.TradeLimitConf;
	import com.ngame.core.conf.data.TradeMarketConf;
	import com.ngame.core.conf.data.TrainerConf;
	import com.ngame.core.conf.data.TransferProConf;
	import com.ngame.core.conf.data.WorldBossConf;
	import com.ngame.core.conf.data.ZdSkillConf;
	import com.ngame.core.conf.data.ZldtConf;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class WXConfEditor extends Sprite
	{
		private var netStream:URLStream;
		
		// 配置文件来源（true服务器，false本地）
		private var fromServ:Boolean = false;
		// 是否修改配置
		private var modifyConf:Boolean = false;
		// 是否保存配置
		private var saveConf:Boolean = false;
		
		public function WXConfEditor()
		{
			setup();
			
			var urlReq:URLRequest = new URLRequest();
			netStream = new URLStream();
			
			if (fromServ) {
				// 服务器配置地址
				urlReq.url = "http://wx.20hb.com/c9/NGame/config/config.data";
				trace("从服务器读取文件");
			} else {
				// 本地配置地址
				urlReq.url = "file://E:/config.data";
				trace("读取本地配置文件");
			}
			
			// 加载配置文件
			netStream.addEventListener(Event.COMPLETE, onConfigLoaded);
			netStream.load(urlReq);
			trace("正在加载配置文件...");
		}
		
		private function onConfigLoaded(event:Event):void
		{
			trace("配置文件加载完毕");
			
			var confData:ByteArray = new ByteArray();
			netStream.readBytes(confData);
			trace("配置文件读取完毕");
			
			try {
				confData.uncompress();
				confData.position = 0;
				var configObj:* = confData.readObject();
				for (var prop:String in configObj) {
					onSubConfig(prop, configObj[prop], configObj);
				}
				trace("配置读取完毕");
				onConfigProcessed(configObj);
			} catch (e:Error) {
				trace(e.message);
			}
		}
		
		private function onSubConfig(name:String, conf:*, rootConf:*):void {
			///////////////////////////////////////////////////////////////////
			// 导出配置
			//exportConf(name, conf, rooConf);
			
			///////////////////////////////////////////////////////////////////
			// DUMP配置
			switch(name) {
				case "playerLevelExpConf":
					break;
				default:
					return;
			}
			dumpConf(name, conf);
			
			///////////////////////////////////////////////////////////////////
			// 更新配置
			//return;
			switch(name) {
				case "playerLevelExpConf":
					updatePlayerLevelExpConf(conf, rootConf);
					break;
				default:
					return;
			}
			
			
		}
		
		/**
		 * 导出配置
		 */
		private function exportConf(name:String, conf:*, rooConf:*):void {
			trace(JSON.stringify(conf));
			var fileName:String = "E:\\wxConf\\" + name + ".json";
			var fileString:String = "";
			if ((conf as Dictionary) == null) {
				fileString = JSON.stringify(conf);
			} else {
				fileString = "{";
			
				var addDot:Boolean = false;
				for (var key:String in conf) {
					if (addDot) {
						fileString += ",";
					}
					fileString += "\"" + key + "\":" + JSON.stringify(conf[key]);
					if (!addDot) {
						addDot = true;
					}
				}
			
				fileString += "}";
			}
			saveFile(fileName, fileString);
		}
		
		/**
		 * 输出配置
		 */
		private function dumpConf(name:String, conf:*):void {
			if ((conf as Dictionary) == null) {
				trace(JSON.stringify(conf));
			} else {
				var confDic:Dictionary = conf as Dictionary;
				for (var key:String in confDic) {
					trace(key + " - " + JSON.stringify(confDic[key]));
				}
			}
		}
		
		/**
		 * 更新角色等级经验配置
		 */
		private function updatePlayerLevelExpConf(conf:*, rootConf:*): void {
			var file:File = new File("D:\\Git\\wxConf\\playerLevelExpConf.json");
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var fileString:String = fileStream.readMultiByte(fileStream.bytesAvailable, "utf-8");
			var rawJson:Array = JSON.parse(fileString) as Array;
			var newArr:Array = new Array();
			for (var index:uint = 0; index < rawJson.length; ++index) {
				var rawRecord:Object = rawJson[index];
				var newItem:PlayerConf = new PlayerConf();
				newItem.level = rawRecord.level;
				newItem.career = rawRecord.career;
				newItem.nextLevel = rawRecord.nextLevel;
				newItem.exp = rawRecord.exp;
				newItem.pro = rawRecord.pro;
				
				newArr.push(newItem);
			}
			
			rootConf["playerLevelExpConf"] = newArr;
			
			trace("playerLevelExpConf 配置更新完毕");
		}
		
		private function onConfigProcessed(confObj:*):void {	
			
			if (!saveConf) {
				return;
			}
			
			// 保存配置
			var confFile:File = new File("E:\\config.data");
			
			var confFileStream:FileStream = new FileStream();
			confFileStream.open(confFile, FileMode.WRITE);
		
			var confData:ByteArray = new ByteArray();
			confData.writeObject(confObj);
			confData.position = 0;
			confData.compress();
			
			confFileStream.writeBytes(confData);
			confFileStream.close();
			
			trace("配置修改完毕");
		}
		
		private function saveFile(filename:String, data:String):void {	
			// 保存配置
			var file:File = new File(filename);
			
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			
			fileStream.writeUTFBytes(data);
			fileStream.close();
			
			trace(filename + "写入成功");
		}
		
		/**
		 * 添加一条商店道具
		 */
		private function addShopItem(shopConf:Dictionary, 
									 page:int, 
									 tag:int, 
									 diamond:int, 
									 shopId:int, 
									 limitNum:int, 
									 notice:int, 
									 money:int, 
									 itemId:String, 
									 bindDiamond:int, 
									 pos:int, 
									 extItemId:int, 
									 openLevel:int):void {
			var shopItem:ShopConf = new ShopConf();
			shopItem.bindDiamond = bindDiamond;
			shopItem.diamond = diamond;
			shopItem.extItemId = extItemId;
			shopItem.itemId = itemId;
			shopItem.limitNum = limitNum;
			shopItem.money = money;
			shopItem.notice = notice;
			shopItem.openLevel = openLevel;
			shopItem.page = page;
			shopItem.pos = pos;
			shopItem.shopId = shopId;
			shopItem.tag = tag;
			
			shopConf[shopId] = shopItem;
		}
		
		public function setup():void
		{
			flash.net.registerClassAlias("data.item", com.ngame.core.conf.data.ItemConf);
			flash.net.registerClassAlias("data.mapConf", com.ngame.core.conf.data.MapConf);
			flash.net.registerClassAlias("data.jumpConf", com.ngame.core.conf.data.MapEntryConf);
			flash.net.registerClassAlias("data.playerAttributeConf", com.ngame.core.conf.data.FighterPropertyConf);
			flash.net.registerClassAlias("data.gameTask", com.ngame.core.conf.data.GameTaskConf);
			flash.net.registerClassAlias("data.dailyTask", com.ngame.core.conf.data.DailyTaskConf);
			flash.net.registerClassAlias("data.dailyTaskReward", com.ngame.core.conf.data.DailyTaskRewardConf);
			flash.net.registerClassAlias("data.npcConf", com.ngame.core.conf.data.NpcConf);
			flash.net.registerClassAlias("data.playerConf", com.ngame.core.conf.data.PlayerConf);
			flash.net.registerClassAlias("data.skillProcConf", com.ngame.core.conf.data.SkillProcConf);
			flash.net.registerClassAlias("data.zdSkillConf", com.ngame.core.conf.data.ZdSkillConf);
			flash.net.registerClassAlias("data.bdSkillConf", com.ngame.core.conf.data.BdSkillConf);
			flash.net.registerClassAlias("data.fzSkillConf", com.ngame.core.conf.data.FzSkillConf);
			flash.net.registerClassAlias("data.szSkillConf", com.ngame.core.conf.data.SzSkillConf);
			flash.net.registerClassAlias("data.instanceConf", com.ngame.core.conf.data.InstanceConf);
			flash.net.registerClassAlias("data.trainingConf", com.ngame.core.conf.data.TrainerConf);
			flash.net.registerClassAlias("data.wuxunConf", com.ngame.core.conf.data.RealmConf);
			flash.net.registerClassAlias("data.fashionConf", com.ngame.core.conf.data.FashionConf);
			flash.net.registerClassAlias("data.fashionLvConf", com.ngame.core.conf.data.FashionUplevelConf);
			flash.net.registerClassAlias("data.transferConf", com.ngame.core.conf.data.TransferProConf);
			flash.net.registerClassAlias("data.errorCodeConf", com.ngame.core.conf.data.ErrorCodeConf);
			flash.net.registerClassAlias("data.buffConf", com.ngame.core.conf.data.BuffConf);
			flash.net.registerClassAlias("data.strongCostConf", com.ngame.core.conf.data.StrongCostConf);
			flash.net.registerClassAlias("data.strongProConf", com.ngame.core.conf.data.StrongProConf);
			flash.net.registerClassAlias("data.fushuConf", com.ngame.core.conf.data.FushuConf);
			flash.net.registerClassAlias("data.fushuUplevelConf", com.ngame.core.conf.data.FushuUplevelConf);
			flash.net.registerClassAlias("data.guildLvConf", com.ngame.core.conf.data.GuildConf);
			flash.net.registerClassAlias("data.guildContributeConf", com.ngame.core.conf.data.GuildDonateConf);
			flash.net.registerClassAlias("data.exchangeShopConf", com.ngame.core.conf.data.ExchangeShopConf);
			flash.net.registerClassAlias("data.guildTaskConf", com.ngame.core.conf.data.GuildTaskConf);
			flash.net.registerClassAlias("data.guildTaskReward", com.ngame.core.conf.data.GuildTaskRewardConf);
			flash.net.registerClassAlias("data.zldtConf", com.ngame.core.conf.data.ZldtConf);
			flash.net.registerClassAlias("data.worldBossConf", com.ngame.core.conf.data.WorldBossConf);
			flash.net.registerClassAlias("data.petChangejobConf", com.ngame.core.conf.data.PetChangejobConf);
			flash.net.registerClassAlias("data.enchantProConf", com.ngame.core.conf.data.EnchantProConf);
			flash.net.registerClassAlias("data.enchantCostConf", com.ngame.core.conf.data.EnchantCostConf);
			flash.net.registerClassAlias("data.guildSkillConf", com.ngame.core.conf.data.GuildSkillConf);
			flash.net.registerClassAlias("data.jctConf", com.ngame.core.conf.data.JctConf);
			flash.net.registerClassAlias("data.dailyActivityConf", com.ngame.core.conf.data.DailyActivityConf);
			flash.net.registerClassAlias("data.marketTreeConf", com.ngame.core.conf.data.TradeMarketConf);
			flash.net.registerClassAlias("data.messageConf", com.ngame.core.conf.data.MessageConf);
			flash.net.registerClassAlias("data.tradeLimitConf", com.ngame.core.conf.data.TradeLimitConf);
			flash.net.registerClassAlias("data.commonRewardConf", com.ngame.core.conf.data.CommonRewardConf);
			flash.net.registerClassAlias("data.guildBattleRewardConf", com.ngame.core.conf.data.GuildBattleRewardConf);
			flash.net.registerClassAlias("data.ShopConf", com.ngame.core.conf.data.ShopConf);
			flash.net.registerClassAlias("data.marryLvUp", com.ngame.core.conf.data.MarryUpConf);
			flash.net.registerClassAlias("data.marryLove", com.ngame.core.conf.data.MarryLoveConf);
			flash.net.registerClassAlias("data.marryIns", com.ngame.core.conf.data.MarryInsConf);
			flash.net.registerClassAlias("data.titleConf", com.ngame.core.conf.data.TitleConf);
			flash.net.registerClassAlias("data.commonConstantConf", com.ngame.core.conf.data.CommonConstantConf);
			flash.net.registerClassAlias("data.luckyTreasuryShowConf", com.ngame.core.conf.data.LuckyTreasuryShowConf);
			flash.net.registerClassAlias("data.commonRewardConf", com.ngame.core.conf.data.CommonRewardConf);
			flash.net.registerClassAlias("data.openActivityConf", com.ngame.core.conf.data.OpenActivityConf);
			flash.net.registerClassAlias("data.menuAuthorityConf", com.ngame.core.conf.data.MenuAuthorityConf);
			flash.net.registerClassAlias("data.levelInvestConf", com.ngame.core.conf.data.LevelInvestConf);
			flash.net.registerClassAlias("data.moduleLinkConf", com.ngame.core.conf.data.ModuleLinkConf);
			flash.net.registerClassAlias("data.strongGuide", com.ngame.core.conf.data.StrongGuideConf);
			flash.net.registerClassAlias("data.cSkillBufferConf", com.ngame.core.conf.data.CSkillBufferConf);
			flash.net.registerClassAlias("data.itemPackageConf", com.ngame.core.conf.data.ItemPackageConf);
			flash.net.registerClassAlias("data.rewardInfoConf", com.ngame.core.conf.data.RewardInfoConf);
			flash.net.registerClassAlias("data.activeConf", com.ngame.core.conf.data.ActiveConf);
			flash.net.registerClassAlias("data.commonNumPriceConf", com.ngame.core.conf.data.CommonNumPriceConf);
			flash.net.registerClassAlias("data.qyConf", com.ngame.core.conf.data.QyConf);
			flash.net.registerClassAlias("data.battleAnalysisConf", com.ngame.core.conf.data.BattleAnalysisConf);
			flash.net.registerClassAlias("data.levelInvestConf", com.ngame.core.conf.data.LevelInvestConf);
			flash.net.registerClassAlias("data.sceneSkill", com.ngame.core.conf.data.SceneSkillConf);
			flash.net.registerClassAlias("data.systemDescriptionConf", com.ngame.core.conf.data.SystemDescriptionConf);
			flash.net.registerClassAlias("data.sceneMonster", com.ngame.core.conf.data.SceneMonsterConf);
			flash.net.registerClassAlias("data.baseMonster", com.ngame.core.conf.data.MonsterConf);
			flash.net.registerClassAlias("data.monsterSkill", com.ngame.core.conf.data.MonsterSkillConf);
			flash.net.registerClassAlias("data.prompt", com.ngame.core.conf.data.PromptConf);
			flash.net.registerClassAlias("data.composeConf", com.ngame.core.conf.data.ComposeConf);
			flash.net.registerClassAlias("data.itemExtConf", com.ngame.core.conf.data.ItemExtConf);
			flash.net.registerClassAlias("data.skillBuff", com.ngame.core.conf.data.SkillBuffConf);
			flash.net.registerClassAlias("data.gameStep", com.ngame.core.conf.data.GameStepConf);
		}
	}
}